//
//  ZYPurchaseDataBase.h
//  ZYPurchase
//
//  Created by wu on 2020/8/10.
//  Copyright © 2020 wu. All rights reserved.
//

#import <Foundation/Foundation.h>

@class ZYPurchaseTransactionModel;
@class FMDatabaseQueue;

NS_ASSUME_NONNULL_BEGIN

@interface ZYPurchaseDataBase : NSObject

// 重置上一次内购 applicationUsername
+ (BOOL)resetLastIAPApplicationUsernameInKeychain:(nonnull NSString *)applicationUsername;
// 清空 applicationUsername
+ (BOOL)clearLastIAPApplicationUsernameInKeychain;
// 获取上次设置的 applicationUsername
+ (nullable NSString *)lastIAPApplicationUsernameInKeychain;
// 购买模块专用文件夹
+ (NSString *)purchaseDirectoryPath;
// 购买收据专用文件夹
+ (NSString *)purchaseReceiptDirectoryPath;
// log文件专用文件夹
+ (NSString *)logFileDirectoryPath;

@property (nonatomic, strong) FMDatabaseQueue *dbQueue;




//////////////////////////////  交易


/// 查询某条交易是否存在
/// @param orderId 订单id
- (BOOL)existTransactionModel:(nonnull NSString *)orderId;



/// 添加交易
/// @param transactionModel 新的校验模型
- (ZYPurchaseTransactionModel *)insertTransactionModel:(nonnull ZYPurchaseTransactionModel *)transactionModel;

/// 删除交易
/// @param orderId 订单id
- (BOOL)deleteTransactionModel:(nonnull NSString *)orderId;

/// 更新交易状态信息
/// @param transactionModel 新的校验模型
- (BOOL)updateTransactionModelStatus:(nonnull ZYPurchaseTransactionModel *)transactionModel;

/// 获取本地所有的交易
- (NSArray <ZYPurchaseTransactionModel *>*)queryAllTransactionModels;
/// 获取本地所有的交易（未校验收据的）
- (NSArray <ZYPurchaseTransactionModel *>*)queryAllTransactionModelsUnVerifyReceipt;
/// 获取单条交易
/// @param orderId 订单id
- (ZYPurchaseTransactionModel *)queryTransactionModel:(nonnull NSString *)orderId;
/// 获取单条订单的交易状态
/// @param orderId 订单id
- (NSInteger)queryTransactionModelTransactionStatus:(nonnull NSString *)orderId;
/// 获取单条交易
/// @param transactionId 交易id
- (ZYPurchaseTransactionModel *)queryTransactionModelByTransactionId:(nonnull NSString *)transactionId;

@end

@interface ZYPurchaseDataBase ()

@end

@interface ZYPurchaseDataBase (RestoreTransaction)

// 获取所有原始交易id
- (NSArray *)allOriginTransactionIdInApple;

// 检测是否需要，新增或修改交易信息
- (BOOL)cacheTransactionIfNeeded:(NSString *)transId
                   originTransId:(NSString *)originTransId
                       productId:(NSString *)productId
              transDateTimestamp:(double)timestamp;

- (BOOL)cacheTransaction:(NSString *)transId
           originTransId:(NSString *)originTransId
               productId:(NSString *)productId
      transDateTimestamp:(double)timestamp;

// 查找此原始交易id对应的交易信息
- (nullable NSDictionary *)fetchTransactionInfoByOriginTransId:(NSString *)originTransId;

@end

@interface ZYPurchaseDataBase (Receipt)

//////////////////////////////  暂存收据

/// 获取所有暂存未校验成功的收据   [查]
- (nullable NSArray *)fetchUnVerifyReceipts;
/// 查看收据是否已经存在
- (BOOL)receiptsFileNameExist:(NSString *)receiptFileName;

/// 移除收据，校验成功后     [删]
/// @param receiptId receiptId
- (BOOL)reVerifyReceiptsSuccess:(NSString *)receiptFileName;


/// 暂存未校验的收据   [增]
/// @param receiptFileName 收据文件名
/// @param orderId 订单id
- (BOOL)storageUnVerifyReceipt:(nonnull NSString *)receiptFileName
                       orderId:(nullable NSString *)orderId;

@end

NS_ASSUME_NONNULL_END
